<script setup lang="ts">
import { getHomeRecommandAPI } from '@/apis/home';
import type { PageParams } from '@/types/global';
import type { RecommandItem } from '@/types/home';
import { onShow } from '@dcloudio/uni-app';
import { ref, toRaw } from 'vue';

// 分页参数
const pageParams: Required<PageParams> = {
    page: 1,
    pageSize: 10,
};

// 推荐商品列表
const recommandList = ref<RecommandItem[]>();

// 当前加载的任务
const loading = ref<UniApp.RequestTask | null>(null);

// 是否加载完成全部数据
let finish = false;

// 加载更多推荐商品
const loadMore = async () => {
    // 判断是否有更多加载
    if (finish === true) return uni.showToast({ icon: 'none', title: '当前没有更多商品推荐~' });
    // 防止重复触发加载
    if (!loading.value) {
        // 请求数据
        try {
            const { task, request } = getHomeRecommandAPI(pageParams);
            // 开始加载
            loading.value = task;
            const response = await request;
            // 第一页为刷新，否则为加载
            if (pageParams.page === 1) recommandList.value = response.result.items;
            else recommandList.value?.push(...response.result.items);
            // 调整分页参数
            finish = pageParams.page >= response.result.pages;
            if (pageParams.page < response.result.pages) pageParams.page++;
        }
        finally {
            // 停止加载
            loading.value = null;
        }
    }
};

// 刷新数据
const onRefresh = async () => {
    // 中断未完成的请求
    toRaw(loading.value)?.abort();
    loading.value = null;
    // 重置数据
    pageParams.page = 1;
    finish = false;
    await loadMore();
};

// 初始化数据
onShow(() => {
    if (!recommandList.value) loadMore();
});

defineExpose({ loadMore, onRefresh });
</script>

<template>
    <!-- 猜你喜欢 -->
    <view class="caption">
        <text class="text">猜你喜欢</text>
    </view>
    <view class="guess">
        <navigator class="guess-item" v-for="item in recommandList" :key="item.id"
            :url="`/pages/good/good?id=${item.id}`">
            <image class="image" mode="aspectFill" :src="item.picture" />
            <view class="name">{{ item.name }}</view>
            <view class="price">
                <text class="small">￥</text>
                <text>{{ item.price }}</text>
            </view>
        </navigator>
    </view>
    <view class="loading-text">
        <text v-if="!loading">--已经到底了--</text>
        <template v-else>
            <XtxLoadingIcon /><text>正在加载...</text>
        </template>
    </view>
</template>

<style lang="scss" scoped>
.caption {
    display: flex;
    justify-content: center;
    line-height: 1;
    padding: 30rpx 0 20rpx;
    font-size: 32rpx;
    color: #262626;

    .text {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0 28rpx 0 30rpx;

        &::before,
        &::after {
            content: '';
            width: 20rpx;
            height: 20rpx;
            background-image: url(@/static/images/bubble.png);
            background-size: contain;
            margin: 0 10rpx;
        }
    }
}

.guess {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 0 20rpx;

    .guess-item {
        width: 49%;
        padding: 24rpx 20rpx 20rpx;
        margin-bottom: 20rpx;
        border-radius: 10rpx;
        overflow: hidden;
        background-color: #fff;
    }

    .image {
        width: 304rpx;
        height: 304rpx;
    }

    .name {
        height: 75rpx;
        margin: 10rpx 0;
        font-size: 26rpx;
        color: #262626;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .price {
        line-height: 1;
        padding-top: 4rpx;
        color: #cf4444;
        font-size: 26rpx;
    }

    .small {
        font-size: 80%;
    }
}

.loading-text {
    text-align: center;
    font-size: 28rpx;
    color: #666;
    padding: 20rpx 0;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>